package com.admin.education.models;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.repositories.Identifiable;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "user_information")
@EntityListeners(AuditingEntityListener.class)
@Data
public class UserInformation implements Serializable,Identifiable{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @NotNull
    @JSONField(serialize=false)
    private  User user;
    @Column(name = "name")
    @Length(max = 128)
    @NotNull
    private String name;
    @Column(name = "student_id")
    @Length(max = 128)
    @NotNull
    private String studentId;
    @Column(name = "head_img_path")
    @Length(max = 1024)
    @NotNull
    private String headImgPath;
    @OneToOne(fetch = FetchType.LAZY)
    @NotNull
    @JSONField(serialize=false)
    private Classes classes;
    @OneToOne(fetch = FetchType.LAZY)
    @NotNull
    @JSONField(serialize=false)
    private Schools schools;
        @OneToOne(fetch = FetchType.LAZY)
        @NotNull
    @JSONField(serialize=false)
    private Role roles;
    @Column(name="valid")
    @NotNull
    private boolean valid;
    @Column(name="last_login_time")
    private Date lastLoginTime;
    @OneToMany(mappedBy = "userInformation")
    @JSONField(serialize=false)
    private List<UserAnswerSheet> userAnswerSheets;
    @Column(name="status")
    @NotNull
    private UserInformationStatus status;
    @OneToOne(mappedBy = "userInformation")
    @JSONField(serialize=false)
    private Integrals integrals;
    @OneToMany(mappedBy = "attention")
    @JSONField(serialize=false)
    private List<AttentionAndFans> attentions;
    @OneToMany(mappedBy = "fan")
    @JSONField(serialize=false)
    private List<AttentionAndFans> fans;
    @OneToMany(mappedBy = "userInformation")
    @JSONField(serialize = false)
    private List<UserRecords> userRecords;
    public boolean checkStatus() throws BadRequestException {
        switch (this.status){
            case NoPass:
                throw new BadRequestException("审核不通过，不能登陆");
            case WaitAudit:
                throw new BadRequestException("账号未审核，不能登陆");
            case Normal:
                return true;
        }
        return  true;
    }
    public enum UserInformationStatus{
        WaitAudit,
        NoPass,
        Normal
    }
}
